package com.flute.framework.autoprompt;

import java.util.HashMap;

public class TrieNode {
	private HashMap<Character, TrieNode> children;
	private WordResult result;

	public TrieNode() {
		this.children = new HashMap<Character, TrieNode>();
	}

	public TrieNode getChildNode(char c) {
		return children.get(c);
	}

	public boolean containsChild(char c) {
		return children.containsKey(c);
	}

	public void addResult(String string, WordResult result) {
		char[] cs = string.toCharArray();
		TrieNode node = this;

		if (!node.containsChild(cs[0])) {
			node.addChild(cs[0], new TrieNode());
		}

		TrieNode childNode = node.getChildNode(cs[0]);

		if (cs.length == 1) {
			this.result = result;
		} else {
			childNode.addResult(string.substring(1), result);
		}
	}

	// private List<TrieNode> arrayToList() {
	// List<TrieNode> list = new ArrayList<TrieNode>();
	// for (int i = 0; i < children.length; i++) {
	// list.add(children[i]);
	// }
	//
	// return list;
	// }
	//
	// private void listToArray(List<TrieNode> list) {
	// TrieNode[] tns = new TrieNode[list.size()];
	// for (int i = 0; i < tns.length; i++) {
	// tns[i] = list.get(i);
	// }
	//
	// children = tns;
	// }

	public void addChild(char key, TrieNode node) {
		children.put(key, node);
	}

	public WordResult getResult() {
		return result;
	}

	public void setResult(WordResult result) {
		this.result = result;
	}

	public String toString() {
		return children + " result:" + result;
	}

	// private Comparator<TrieNode> getComparator() {
	// return new Comparator<TrieNode>() {
	//
	// @Override
	// public int compare(TrieNode o1, TrieNode o2) {
	// if (o1.value == o2.value) {
	// return 0;
	// } else if (o1.value > o2.value) {
	// return 1;
	// } else {
	// return -1;
	// }
	// }
	// };
	// }
}
